import Hashes from 'jshashes'

let MD5 = new Hashes.MD5()
const APPID = 'APMSrJZ6WGKUtOnstol7XYa7jxjd7BR4'
const KEY = 'a563ee0424e00dda4b58a559891185dd'

export default {
  getOrders (queryStartTime, queryEndTime) {
    return new Promise((resolve, reject) => {
      let timeStamp = +new Date()
      let params = {
        appId: APPID,
        queryEndTime: queryEndTime,
        queryStartTime: queryStartTime,
        timestamp: timeStamp
      }
      let query = []
      for (let item in params) {
        query.push(`${item}=${params[item]}`)
      }
      params.sign = MD5.setUpperCase(true).hex_hmac(KEY, query.join('&'))

      // let tempForm = new window.FormData()
      // for (let item in params) {
      //   tempForm.append(item, params[item])
      // }
      let queryList = []

      for (let key in params) {
        queryList.push(key + '=' + params[key])
      }

      let queryStr = '?' + queryList.join('&')

      let xmlhttp = new window.XMLHttpRequest()
      xmlhttp.onreadystatechange = function onreadystateChange () {
        if (xmlhttp.readyState === 4) { // 4 = "loaded"
          if (xmlhttp.status === 200) { // 200 = OK
            resolve({
              data: JSON.parse(xmlhttp.responseText),
              status: 200
            })
          } else {
            reject()
          }
        }
      }
      xmlhttp.open('GET', `http://api-test.xlink.cn:808/poly/park/orders${queryStr}`)
      // xmlhttp.send(tempForm)
      xmlhttp.send()
    })
  }
}
